package com.anfeng.cms.controller;

import com.anfeng.cms.common.SessionUtil;
import com.anfeng.cms.domain.attendance.AfRosterDetail;
import com.anfeng.cms.domain.attendance.AfRosterInfo;
import com.anfeng.cms.domain.emp.AfEmployeeInfo;
import com.anfeng.cms.domain.emp.AfFaceInfo;
import com.anfeng.cms.domain.emp.AfGuestInfo;
import com.anfeng.cms.domain.emp.AfGuestLog;
import com.anfeng.cms.domain.org.AfDepartment;
import com.anfeng.cms.request.emp.*;
import com.anfeng.cms.request.manager.ManagerUser;
import com.anfeng.cms.service.attendance.IAfRosterDetailService;
import com.anfeng.cms.service.attendance.IAfRosterInfoService;
import com.anfeng.cms.service.emp.*;
import com.anfeng.cms.service.org.IAfDepartmentService;
import com.anfeng.cms.util.time.WorkAgeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 吴朋
 * @date 2019/7/16
 */
@Controller
@Slf4j
public class EmployeeController {

    @Autowired
    private IAfEmployeeInfoService employeeInfoService;
    @Autowired
    private IAfDepartmentService departmentService;
    @Autowired
    private IAfGuestInfoService guestInfoService;
    @Autowired
    private IAfFaceInfoService faceInfoService;
    @Autowired
    private IAfRosterInfoService rosterInfoService;
    @Autowired
    private IAfRosterDetailService rosterDetailService;
    @Autowired
    private IAfEmployeePhotoService employeePhotoService;
    @Autowired
    private IAfGuestLogService guestLogService;
    @Autowired
    private UploadImageService imageService;

    /**
     * 员工管理
     * @param req 请求参数
     * @param employeeMap 返回对象
     * @return 跳转页面
     */
    @GetMapping("userManage/user")
    public String employeList(EmployeeInfoReq req, Map<String, Object> employeeMap) {
        if (null == req) {
            req = new EmployeeInfoReq();
        }
        if (req.getPage() == null || req.getPage() < 1) {
            req.setPage(1);
        }
        if (req.getPageSize() == null || req.getPageSize() < 10) {
            req.setPageSize(10);
        }
        employeeMap.put("current_page", req.getPage());
        employeeMap.put("size", req.getPageSize());
        int infoCounts = employeeInfoService.getEmployeeInfoCounts(req);
        int total = (infoCounts - 1) / req.getPageSize() + 1;
        employeeMap.put("total_page", total);
        employeeMap.put("totals", infoCounts);
        List<EmployeeInfoDTO> infoDTOS = new ArrayList<>();
        if (infoCounts > 0) {
            List<AfEmployeeInfo> employeeInfos = employeeInfoService.getEmployeeInfosByPage(req, req.getPage(), req.getPageSize());
            if (null != employeeInfos && !employeeInfos.isEmpty()) {
                infoDTOS = employeeInfos.stream().map(employeeInfo -> {
                    EmployeeInfoDTO dto = new EmployeeInfoDTO();
                    BeanUtils.copyProperties(employeeInfo, dto);
                    //补充工龄
                    int workAge = WorkAgeUtil.getWorkAgeLength(employeeInfo.getHireDate(), employeeInfo.getDismissalDate());
                    dto.setWorkAge(workAge);
                    if(StringUtils.isNotBlank(employeeInfo.getDeptId())) {
                        AfDepartment departmentInfo = departmentService.getDepartmentInfo(employeeInfo.getDeptId());
                        if (null != departmentInfo) {
                            dto.setDeptName(departmentInfo.getName());
                        }
                    }
                    return dto;
                }).collect(Collectors.toList());
            }
        }
        employeeMap.put("datas", infoDTOS);
        // 获取组织部门 TODO
        departmentService.getOrganizationTree();
        return "userManage/user";
    }

    /**
     * 人脸管理
     * @param faceInfoReq 参数
     * @param faceMap 对象
     * @return 页面
     */
    @GetMapping("/userManage/faceManage")
    public String faceManager(EmployeeFaceInfoReq faceInfoReq, Map<String, Object> faceMap) {
        if (null == faceInfoReq) {
            faceInfoReq = new EmployeeFaceInfoReq();
        }
        if (faceInfoReq.getPage() == null || faceInfoReq.getPage() < 1) {
            faceInfoReq.setPage(1);
        }
        if (faceInfoReq.getPageSize() == null || faceInfoReq.getPageSize() < 10) {
            faceInfoReq.setPageSize(10);
        }
        faceMap.put("current_page", faceInfoReq.getPage());
        faceMap.put("size", faceInfoReq.getPageSize());
        int infoCounts = faceInfoService.getFaceInfoCounts(faceInfoReq);
        int total = (infoCounts - 1) / faceInfoReq.getPageSize() + 1;
        faceMap.put("total_page", total);
        faceMap.put("totals", infoCounts);
        List<EmployeeFaceInfoDTO> infoDTOS = new ArrayList<>();
        if (infoCounts > 0) {
            List<AfFaceInfo> guestInfos = faceInfoService.getFaceInfosByPage(faceInfoReq, faceInfoReq.getPage(), faceInfoReq.getPageSize());
            if (null != guestInfos && !guestInfos.isEmpty()) {
                infoDTOS = guestInfos.stream().map(faceInfo -> {
                    EmployeeFaceInfoDTO dto = new EmployeeFaceInfoDTO();
                    BeanUtils.copyProperties(faceInfo, dto);
                    if(StringUtils.isNotBlank(faceInfo.getDeptId())) {
                        AfDepartment departmentInfo = departmentService.getDepartmentInfo(faceInfo.getDeptId());
                        if (null != departmentInfo) {
                            dto.setDeptName(departmentInfo.getName());
                        }
                    }
                    return dto;
                }).collect(Collectors.toList());
            }
        }
        faceMap.put("datas", infoDTOS);
        return "userManage/faceManage";
    }

    /**
     * 访客管理
     * @param guestInfoReq 参数
     * @param guestMap 对象
     * @return 跳转页面
     */
    @GetMapping("/userManage/Visitor")
    public String visitorList(GuestInfoReq guestInfoReq, Map<String, Object> guestMap) {
        if (null == guestInfoReq) {
            guestInfoReq = new GuestInfoReq();
        }
        if (guestInfoReq.getPage() == null || guestInfoReq.getPage() < 1) {
            guestInfoReq.setPage(1);
        }
        if (guestInfoReq.getPageSize() == null || guestInfoReq.getPageSize() < 10) {
            guestInfoReq.setPageSize(10);
        }
        guestMap.put("current_page", guestInfoReq.getPage());
        guestMap.put("size", guestInfoReq.getPageSize());
        int infoCounts = guestInfoService.getAllGuestInfoCounts(guestInfoReq);
        int total = (infoCounts - 1) / guestInfoReq.getPageSize() + 1;
        guestMap.put("total_page", total);
        guestMap.put("totals", infoCounts);
        List<GuestInfoDTO> infoDTOS = new ArrayList<>();
        if (infoCounts > 0) {
            List<AfGuestInfo> guestInfos = guestInfoService.getGuestInfoByPage(guestInfoReq, guestInfoReq.getPage(), guestInfoReq.getPageSize());
            if (null != guestInfos && !guestInfos.isEmpty()) {
                infoDTOS = guestInfos.stream().map(guestInfo -> {
                    GuestInfoDTO dto = new GuestInfoDTO();
                    BeanUtils.copyProperties(guestInfo, dto);
                    return dto;
                }).collect(Collectors.toList());
            }
        }
        guestMap.put("datas", infoDTOS);
        return "userManage/Visitor";
    }

    @GetMapping("/userManage/Scheduling")
    public String scheduleList(RosterInfoReq rosterInfoReq, Map<String, Object> scheduleMap) {
        if (null == rosterInfoReq) {
            rosterInfoReq = new RosterInfoReq();
        }
        if (rosterInfoReq.getPage() == null || rosterInfoReq.getPage() < 1) {
            rosterInfoReq.setPage(1);
        }
        if (rosterInfoReq.getPageSize() == null || rosterInfoReq.getPageSize() < 10) {
            rosterInfoReq.setPageSize(10);
        }
        scheduleMap.put("current_page", rosterInfoReq.getPage());
        scheduleMap.put("size", rosterInfoReq.getPageSize());
        int infoCounts = rosterInfoService.getAfRosterInfoCounts(rosterInfoReq);
        int total = (infoCounts - 1) / rosterInfoReq.getPageSize() + 1;
        scheduleMap.put("total_page", total);
        scheduleMap.put("totals", infoCounts);
        List<RosterInfoDTO> infoDTOS = new ArrayList<>();
        if (infoCounts > 0) {
            List<AfRosterInfo> rosterInfos = rosterInfoService.getRosterInfosByPage(rosterInfoReq, rosterInfoReq.getPage(), rosterInfoReq.getPageSize());
            if (null != rosterInfos && !rosterInfos.isEmpty()) {
                infoDTOS = rosterInfos.stream().map(rosterInfo -> {
                    RosterInfoDTO dto = new RosterInfoDTO();
                    dto.setId(rosterInfo.getId());
                    dto.setCross(rosterInfo.getCross());
                    dto.setName(rosterInfo.getName());
                    int persons = rosterDetailService.getRosterInfoDetailCounts(rosterInfo.getId());
                    dto.setPersons(persons);
                    return dto;
                }).collect(Collectors.toList());
            }
        }
        scheduleMap.put("datas", infoDTOS);
        return "userManage/Scheduling";
    }

    @GetMapping("userManage/add")
    public String addEmployeePage() {
        return "userManage/operation/add";
    }
    @GetMapping("userManage/changeDept")
    public String changeEmployeeDept() {
        return "userManage/operation/trimManage";
    }
    @GetMapping("userManage/batchLoadPic")
    public String batchLoadPic() {
        return "userManage/operation/loadPic";
    }
    @GetMapping("userManage/batchImport")
    public String batchImportInfo() {
        return "userManage/operation/Import";
    }

    @GetMapping("userManage/batchLoadFace")
    public String batchLoadFacePic() {
        return "userManage/operation/loadFace";
    }

    @GetMapping("userManage/face/detail")
    public String faceInfoDetail(@RequestParam("emp_no") String empNo, Map<String, Object> modelMap) {
        if (StringUtils.isBlank(empNo)) {
            return null;
        }
        AfFaceInfo afFaceInfo = faceInfoService.getFaceInfoByEmpNo(empNo);
        if (afFaceInfo == null) {
            return null;
        }
        modelMap.put("info", afFaceInfo);
        // 获取图片地址
        List<EmployeePhotoDTO> employeePhotoDTOs = employeePhotoService.getEmployeePhotos(afFaceInfo.getEmpId());
        if (null != employeePhotoDTOs) {
            List<EmployeePhotoDTO> otherPhotos = new ArrayList<>();
            for (EmployeePhotoDTO dto : employeePhotoDTOs) {
                if (dto.getId().equals(afFaceInfo.getPhotoId())) {
                    modelMap.put("basePic", dto);
                } else {
                    otherPhotos.add(dto);
                }
            }
            modelMap.put("otherPic", otherPhotos);
        }
        return "userManage/operation/faceInfo";
    }

    @GetMapping("userManage/upload/guest/photo")
    public String uploadGueestPhoto() {
        return "userManage/operation/loadGuest";
    }

    @GetMapping("userManage/guest/add")
    public String addGuestInfo() {
        return "userManage/operation/addGuest";
    }

    @GetMapping("userManage/guest/log")
    public String guestInfoLog(@RequestParam("guestId") String guestId,Map<String, Object> modelMap) {
        if (StringUtils.isBlank(guestId)) {
            return null;
        }
        GuestLogReq req = new GuestLogReq();
        req.setGuestId(guestId);
        // 获取流水
        List<AfGuestLog> allGuestLogs = guestLogService.getAllGuestLogs(req);
        modelMap.put("datas", allGuestLogs);
        return "userManage/operation/guestLog";
    }

    @GetMapping("userManage/guest/showpic")
    public String showGuestPhoto(@RequestParam("photoId")String photoId, Map<String, String> modelMap) {
        if (StringUtils.isBlank(photoId)) {
            return null;
        }
        String url = imageService.getImageUrlById(photoId);
        modelMap.put("url", url);
        return "userManage/operation/showPic";
    }

    @GetMapping("userManage/user/edit")
    public String editEmployee(@RequestParam("id")String id, Map<String, Object> objectMap) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        AfEmployeeInfo employeeInfo = employeeInfoService.selectById(id);
        if (null == employeeInfo) {
            return null;
        }
        objectMap.put("data", employeeInfo);
        return "userManage/operation/editEmp";
    }

    @GetMapping("userManage/schedule/create")
    public String createSchedule() {
        return "userManage/operation/setClass";
    }

    @GetMapping("userManage/schedule/list")
    public String scheduleList(@RequestParam("id")String id, Map<String, Object> objectMap) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        List<AfRosterDetail> infoDetails = rosterDetailService.getRosterInfoDetails(id);
        objectMap.put("datas", infoDetails);
        String rosterId="";
        if (null != infoDetails && !infoDetails.isEmpty()) {
            rosterId = infoDetails.get(0).getRosterId();
        }
        objectMap.put("rosterId", rosterId);
        return "userManage/operation/userTable";
    }

    @GetMapping("userManage/schedule/change/trim")
    public String trimChangeClass(@RequestParam("id")String rosterId, Map<String, Object> objectMap) {
        if (StringUtils.isBlank(rosterId)) {
            return null;
        }
        AfRosterInfo rosterInfo = rosterInfoService.selectById(rosterId);
        if (null == rosterInfo) {
            return null;
        }

        List<AfRosterDetail> rosterInfoDetails = rosterDetailService.getRosterInfoDetails(rosterId);
        if (null == rosterInfoDetails || rosterInfoDetails.isEmpty()) {
            objectMap.put("persons", null);
        } else {
            objectMap.put("persons", rosterInfoDetails);
        }
        objectMap.put("data", rosterInfo);
        return "userManage/operation/temporaryShift";
    }

    @GetMapping(value = "userManage/schedule/edit")
    public String editSchedule(@RequestParam("id")String id, Map<String, Object> objectMap) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        AfRosterInfo rosterInfo = rosterInfoService.selectById(id);
        if (null == rosterInfo) {
            return null;
        }
        List<AfRosterDetail> rosterInfoDetails = rosterDetailService.getRosterInfoDetails(rosterInfo.getId());
        if (null == rosterInfoDetails || rosterInfoDetails.isEmpty()) {
            objectMap.put("persons", null);
        } else {
            objectMap.put("persons", rosterInfoDetails);
        }
        objectMap.put("data", rosterInfo);
        return "userManage/operation/setEdit";
    }

}
